package com.mvp.lt.airlineview.picture;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.compress.Checker;
import com.luck.picture.lib.compress.Engine;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.photoview.PhotoView;
import com.mvp.lt.airlineview.R;
import com.mvp.lt.airlineview.base.BaseActivity;
import com.mvp.lt.airlineview.map.PolygonLineActivity;
import com.mvp.lt.airlineview.utils.GlideUtils;
import com.mvp.lt.airlineview.utils.ImageUtils;
import com.mvp.lt.airlineview.utils.LogUtil;
import com.mvp.lt.airlineview.utils.MainThread;
import com.mvp.lt.airlineview.utils.PictureSelectUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuTao
 * @description: 图片压缩
 * @date : 2021/1/14 17:00
 */
public class PictureInSampleActivity extends BaseActivity {

    private PhotoView mIvOriginal;
    private PhotoView mIvInSample;
    private TextView mTvPath;
    private Button mBtnInSample;
    private Button mBtnChoose;
    private List<LocalMedia> lists = new ArrayList<>();
    private String mLocalMediaPath;


    public static void show(Context context) {
        Intent intent = new Intent(context, PictureInSampleActivity.class);

        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_in_sample);
        mIvOriginal = findViewById(R.id.iv_original);
        mIvInSample = findViewById(R.id.iv_in_sample);
        mTvPath = findViewById(R.id.tv_path);
        mBtnInSample = findViewById(R.id.btn_in_sample);
        mBtnChoose = findViewById(R.id.btn_choose);

        mBtnChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PictureSelectUtils.pictureSelectorFormPhoto(PictureInSampleActivity.this,
                        PictureMimeType.ofImage(), 1,
                        false,
                        PictureConfig.SINGLE,
                        lists,
                        1111);
            }
        });
        mBtnInSample.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1920*1080
                String path = "/storage/emulated/0/Pictures/"+System.currentTimeMillis()+".jpg";
                int mLeastCompressSize = 100;
                    AsyncTask.SERIAL_EXECUTOR.execute(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                File result ;
                                if (Checker.isNeedCompress(mLeastCompressSize, mLocalMediaPath)) {
                                    LogUtil.e("路径", "===========压缩===========");
                                    PictureEngine engine = new PictureEngine(mLocalMediaPath, new File(path),100);
                                    result =  engine.compressFile();
                                }else {
                                    result =new File(mLocalMediaPath);
                                    LogUtil.e("路径", "===========不压缩===========");
                                }
                                MainThread.run(new Runnable() {
                                    @Override
                                    public void run() {
                                        GlideUtils.intoAdImage(result.getAbsolutePath(), mIvInSample);
                                    }
                                });
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
//                ImageUtils.compressBitmap(mLocalMediaPath, path, 1080, new ImageUtils.OnSaveSuccessListener() {
//                    @Override
//                    public void onSuccess(String path) {
//                        GlideUtils.intoAdImage(path, mIvInSample);
//
//                    }
//                });

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1111) {
            lists = PictureSelector.obtainMultipleResult(data);
            if (lists != null) {
                LocalMedia localMedia = lists.get(0);
                mLocalMediaPath = localMedia.getPath();
                LogUtil.e("路径", mLocalMediaPath);
                mTvPath.setText("路径:" + mLocalMediaPath);
                GlideUtils.intoAdImage(mLocalMediaPath, mIvOriginal);
            }
        }

    }
}
